//
//  AllSearchViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/11/29.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AllSearchViewController.h"
#import "CCZSegementController2.h"

#import "LiveContentViewController.h"
#import "MallReccomendViewController.h"
#import "JobRecommendListViewController.h"

@interface AllSearchViewController () <UISearchBarDelegate>

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSString *keyWord;

@property (nonatomic, strong) UISearchBar *searchBar;

// 历史记录
@property (nonatomic, strong) UIView *line1;

@end

@implementation AllSearchViewController{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self buildUI];
}


#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.view.backgroundColor = kWhiteColor;
    
    self.navigationItem.title = @"综合搜索";
    
    self.keyWord = @"";
	
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(SW(20), SW(40), SCREEN_WIDTH-SW(10), SW(10))];
    _searchBar.keyboardType = UIKeyboardTypeDefault;
    _searchBar.delegate = self;
    // 底部的颜色
    _searchBar.barTintColor = [UIColor whiteColor];
    _searchBar.layer.borderWidth = 1;
    _searchBar.layer.borderColor = RGBA(236, 236, 236, 1).CGColor;
    _searchBar.layer.cornerRadius = SW(20);
    _searchBar.layer.masksToBounds = YES;
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    _searchBar.barStyle = UIBarStyleBlack;
    [self.view addSubview:_searchBar];
    [_searchBar becomeFirstResponder];
    
    _line1 = [[UIView alloc] initWithFrame:CGRectMake(0, _searchBar.y+_searchBar.height+SW(40), SCREEN_WIDTH, 1)];
    [self.view addSubview:_line1];
    _line1.backgroundColor = UISeparatedColorRGB;
	
	MallReccomendViewController *mallVC = [[MallReccomendViewController alloc] init];
    
    LiveContentViewController *liveVC = [[LiveContentViewController alloc] init];
    liveVC.isSearch = @"yes";
	
	JobRecommendListViewController *jobVC = [[JobRecommendListViewController alloc] init];
	
	NSArray *nameArr;
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 5 || role == 6) {
		nameArr = @[@"商城", @"直播", @"人才"];
	}
	else {
		nameArr = @[@"商城", @"直播", @"岗位"];
	}
	
    NSArray *vcArr = @[mallVC, liveVC, jobVC];
    
    CCZSegementController2 *segement = [CCZSegementController2 segementControllerWithFrame:CGRectMake(0, _line1.y+1, SCREEN_WIDTH, SCREEN_HEIGHT-64-(_line1.y+1)) titles:nameArr];
    [segement setSegementViewControllers:vcArr];
    
    segement.style = CCZSegementStyleDefault;
    [segement setSegementTintColor:UIMainColorRGB];
    [self.view addSubview:segement.view];
    [self addChildViewController:segement];
    __weak typeof(self) weakSelf = self;
    [segement selectedNavAtIndex:^(NSInteger index) {
        NSLog(@"index %ld",index);
        weakSelf.index = index;
    }];
    
}

#pragma mark ==========UISearchBar 代理响应方法==========
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    return YES;
}
//- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
//    return YES;
//}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    NSLog(@"开始输入搜索内容");
    
    // 取消字体颜色？
    searchBar.showsCancelButton = YES;
    [searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    NSLog(@"输入搜索完毕");
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"取消搜索");
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"搜索");
    
    NSString *word = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if (word.length == 0) {
        [self presentViewController:[LZCTools alertMessage:@"请输入关键字"] animated:YES completion:nil];
        return;
    }
    
    self.keyWord = word;
    
    [self searchB];
    
}

- (void)searchB {
    
    
    if (self.keyWord.length == 0) {
        NSLog(@"没有关键字");
        return;
    }
    if ([_searchBar isFirstResponder]) {
        NSLog(@"没有隐藏键盘");
        [_searchBar resignFirstResponder];
    }
    
    //创建通知
    NSNotification *notification;
	notification =[NSNotification notificationWithName:@"searchLive" object:nil userInfo:@{@"keyWord":self.keyWord}];
	//通过通知中心发送通知
	[[NSNotificationCenter defaultCenter] postNotification:notification];
}

//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"searchPopular" object:nil];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
